<?php
namespace app\h5api\controller;

use app\common\model\DataArea;
use app\common\model\DataCity;
use app\common\model\DataProvince;
use app\common\model\Address as AddressModel;
use think\exception\ValidateException;
use think\Request;

class Address extends Base
{

    /**
     * @route('h5/GetAddressList')
     * 获取用户地址列表
     */
    public function GetAddressList()
    {
        $model = new AddressModel();
        $where = [
            ["user_id",'=',$this->_userid],
            ["status",'=',1],
        ];
        $address = $model->where($where)->order("is_default desc,address_id desc")->select();
        $this->result($address,1,'返回成功');
    }

    /**
     * @route('h5/AddAddress')
     * 用户添加收货地址
     */
    public function AddAddress()
    {
        $param = input();
        $validate = new \app\h5api\validate\Address();
        $res = $validate->scene('EditAddress')->check($param);
        if(!$res){
            $this->result('',-1,$validate->getError());
        }

        $model = new \app\common\model\Address();

        $data = [
            'name' => $param['name'],
            'phone' => $param['phone'],
            'province_id' => $param['province_id'],
            'city_id' => $param['city_id'],
            'area_id' => $param['area_id'],
            'province_name' => $param['province_name'],
            'city_name' => $param['city_name'],
            'area_name' => $param['area_name'],
            'detail' => $param['detail'],
            'is_default' => $param['is_default'],
            'user_id' => $this->_userid,
        ];
        $model->startTrans();
        if($param['is_default'] = 1){
            $thers = $model->save(['is_default' => 0],['user_id' => $this->_userid]);
            if(!$thers){
                $model->rollback();
                $this->result('',-1,$model->getError());
            }
        }

        $rs = $model->create($data);
//        echo $model->getLastSql();die;
        if(!$rs){
            $model->rollback();
            $this->result('',-1,$model->getError());
        }
        $model->commit();
        $this->result($rs,1,'返回成功');
    }

    /**
     * @route('h5/OneAddress')
     * 单个收货地址及编辑
     */
    public function OneAddress()
    {
        $model = new AddressModel();
        $validate = new \app\h5api\validate\Address();
        $param = input();
        $address_id = input('address_id');
        $where = [
            ['address_id','=',$address_id],
            ['user_id','=',$this->_userid],
            ['status','=',1],
        ];
        $theaddress = $model->where($where)->find();
        if(!$theaddress){
            $this->result('',-2,'不存在该收货地址');
        }
        if(input('handle') == 'ajax'){
            $res = $validate->scene('EditAddress')->check($param);
            if(!$res){
                $this->result('',-1,$validate->getError());
            }
            $data = [
                'name' => $param['name'],
                'phone' => $param['phone'],
                'province_id' => $param['province_id'],
                'city_id' => $param['city_id'],
                'area_id' => $param['area_id'],
                'province_name' => $param['province_name'],
                'city_name' => $param['city_name'],
                'area_name' => $param['area_name'],
                'detail' => $param['detail'],
                'is_default' => $param['is_default'],
                'user_id' => $this->_userid,
            ];
            $model->startTrans();
            if($param['is_default'] == 1){
                $thers = $model->allowField(true)->save(['is_default' => 0],['user_id' => $this->_userid]);
                if(!$thers){
                    $model->rollback();
                    $this->result('',-1,$model->getError());
                }
            }
            $rs = $model->edit($data,$address_id);
            if(!$rs){
                $model->rollback();
                $this->result('',-1,$model->getError());
            }
            $model->commit();
            $this->result($rs,1,'返回成功');
        }
        $this->result($theaddress,1,'返回成功');
    }

    /**
     * @route('h5/DelAddress')
     * 用户删除收货地址
     */
    public function DelAddress()
    {
        $model = new AddressModel();
        $address_id = input('address_id');
        $where = [
            ['address_id','=',$address_id],
            ['user_id','=',$this->_userid],
            ['status','=',1],
        ];
        $theaddress = $model->where($where)->find();
        if(!$theaddress){
            $this->result('',-1,'不存在该收货地址');
        }
        $data = [
            'status' => 0,
        ];
        $rs = $model->edit($data,$address_id);
        if(!$rs){
            $this->result('',-1,$model->getError());
        }
        $this->result($rs,1,'返回成功');
    }

    /**
     * @route('h5/SetDefaultAddress')
     * 设置默认收货地址
     */
    public function SetDefaultAddress()
    {
        $model = new AddressModel();
        $param = input();
        $address_id = input('address_id');
        $where = [
            ['address_id','=',$address_id],
            ['user_id','=',$this->_userid],
            ['status','=',1],
        ];
        $theaddress = $model->where($where)->find();
        if(!$theaddress){
            $this->result('',-2,'不存在该收货地址');
        }
        $data = $theaddress->toArray();
        if($data['is_default'] == 1){
            $this->result('',1,'至少需要一条默认地址');
        }
        $param['is_default'] = 1;

        $model->startTrans();
        $thers = $model->allowField(true)->save(['is_default' => 0],['user_id' => $this->_userid]);
        if(!$thers){
            $model->rollback();
            $this->result('',-1,$model->getError());
        }
        $rs = $model->edit($param,$address_id);
        if(!$rs){
            $model->rollback();
            $this->result('',-1,$model->getError());
        }
        $model->commit();
        $this->result($rs,1,'返回成功');
    }

    /**
     * @route('h5/GetDefaultAddress')
     * 用户获取默认收货地址
     */
    public function GetDefaultAddress()
    {
        $model = new AddressModel();
        $where = [
            ['user_id','=',$this->_userid],
            ['status','=',1],
            ['is_default','=',1],
        ];
        $address = $model->getOne($where);
        if(empty($address)){
            $this->result("",-1,'暂无收货地址');
        }
        $this->result($address,1,'返回成功');
    }
}
